package utils;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.Domain;
import dtos.DomainDTO;



/**
 * Session Bean implementation class DomainDAO
 * @author Radulescu Cristina
 * @author Pop Loredana
 */
@Stateless
@LocalBean
public class DomainDAO implements DomainDAORemote {

    /**
     * Default constructor. 
     */
	
	@PersistenceContext
	EntityManager em;

	@EJB
	Conv conv;
	
    public DomainDAO() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Method used to insert a domain in the database
     * @param domain 
     */
    @Override
	public void insertDomain(DomainDTO domain) {
		
    	Domain d=em.find(Domain.class,domain.getId());
    	
		if(d==null){
			Domain d1 = conv.fromEntityDomainDTO(domain);
			em.persist(d1);
		}
		else{
			em.merge(d);
		}
		
	}
    
    /**
     * Method used to update the Domain
     * @param ud
     */
    @Override
    public void updateDomain(DomainDTO ud){
    	if(em.find(Domain.class, ud.getId())!=null)
    	  em.merge(conv.fromEntityDomainDTO(ud));
    }
    
    /**
     * Method used to get the domain by id
     * @param id 
     * @return Domain
     */
    
    @Override
	public DomainDTO getDomain(int id) {
    	return conv.fromEntityDomain(em.find(Domain.class, id));
	}
    
    /**
     * Method used to delete the domain
     * @param ud 
     */
    @Override
    public void deleteDomain(DomainDTO ud){
		Domain r = em.find(Domain.class, ud.getId());
        em.remove(r);
	}
    
    /**
     * Method used to get all domains 
     * @return list of domains
     */
    @Override
    public List<DomainDTO> getAllDomains() {
		List<DomainDTO> resultlist = new ArrayList<>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Domain> cq = cb.createQuery(Domain.class);
		Root<Domain> dom = cq.from(Domain.class);
		cq.select(dom);
		TypedQuery<Domain> q = em.createQuery(cq);

		List<Domain> domains = q.getResultList();
		
		for (Domain d : domains) {
			resultlist.add(conv.fromEntityDomain(d));
		}
		return resultlist;
	}
    
    /**
     * Method used to get the domain by name
     * @return Domain
     */
    
    @Override  
    public DomainDTO getDomainByName(String name) {

        CriteriaBuilder cb = em.getCriteriaBuilder();

                CriteriaQuery<Domain> cq = cb.createQuery(Domain.class);

                Root<Domain> dom = cq.from(Domain.class);

                cq.select(dom);

                TypedQuery<Domain> q = em.createQuery(cq);

                List<Domain> domains = q.getResultList();

                

                for (Domain d : domains) {

                        if (d.getName().equalsIgnoreCase(name)) {

                                return conv.fromEntityDomain(d);

                        }

                }

                return null;

        }


}
